/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.InitialData;
import Simplex.Simplex;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Stiv
 */
public class GoicocheaPanel extends javax.swing.JPanel {
    
   static DefaultTableModel dtmZ;
   static DefaultTableModel dtmG;
   private double epsilon = 0.0;
   static private InitialData initialData = Application.initialData;

    /**
     * Creates new form GoicocheaPanel
     */
    public GoicocheaPanel() {
        initComponents();
        
        resultados();
        algoritmo();
    }
    
    
    
    
    
    //************************************************************************************************************* 
    //*************************************************************************************************************
    //*************************************************************************************************************    
    //*************************************************************************************************************
    //*************************************************************************************************************
    
    //static int numeroVariables = InitialDataPanel.getNumberVariables();
    static int numeroVariables = initialData.getNumberVariables();
    static Map five;
    static Map six;
    static double[][] A = initialData.getRestrictionData();
    static double[] b = initialData.getLdData();
    static double[][] resultados = new double[numeroVariables][numeroVariables];
    static double[][] objetivos = initialData.getObjectiveData();
    static double[] zkMax;
    static double[] zkMin;
    static double[] zMax;
    static double[][] gi;
    static double constant;
    static double[] s;

    //public static void main(String[] args) {
    public static void algoritmo(){
        
        
        


        resultados = applySimplex(objetivos, A, b);

        //System.out.println("Matriz de  Pagos");
        double[][] pagos = calculateOptimalSolutions(resultados, objetivos);
        //for (int i = 0; i < pagos.length; i++) {
          //  for (int j = 0; j < pagos[0].length; j++) {
            //    System.out.print(pagos[i][j] + "\t");
            //}
            //System.out.println("");
        //}

        zkMax = findMax(pagos);
        zkMin = findMin(pagos);
        //System.out.print("\nkMax\t");
        //for (int i = 0; i < zkMax.length; i++) {
        //    System.out.print(zkMax[i] + "\t");
        //}
        //System.out.print("\nkMin\t");
        //for (int i = 0; i < zkMin.length; i++) {
      //      System.out.print(zkMin[i] + "\t");
    //}
        zMax = defineZMax(zkMax);
        //System.out.print("\nZMax\t");
        //for (int i = 0; i < zMax.length; i++) {
        //    System.out.print(zMax[i] + "\t");
        //}
        //System.out.println("\nPaso 5");
        five = calculateStepFive(objetivos, zkMin, zkMax);
        gi = (double[][]) five.get("gi");
        constant = (Double) five.get("constant");
        s = (double[]) five.get("s");

        //System.out.println("\nPaso 6");
        six = calculateStepSix(A, b, s, constant, objetivos, gi, false, null);




    }
    
    
    
    public static void decisionFinal(){
                boolean first = true;
                String constraint = null;
        //do {
            /*if (first) {
                first = false;
            } else {
            * 
            */
                double e;// = 817;
                int desigualdad;// = 0;
                String valorDesigualdad = "";
                if(constraint!=null){
                   boolean flag = true;
                   while(flag){
                       try{
                    valorDesigualdad = JOptionPane.showInputDialog(constraint+"\n"+"Ingrese la desigualdad [ 0: <=, 1: >=, 2: = ]");
                    if(Integer.valueOf(valorDesigualdad)>=0 && Integer.valueOf(valorDesigualdad)<=3) flag = false;
                       }
                       catch(NumberFormatException nfe){
                           flag = true;
                       }
                   }
                    
                }else{
                    boolean flag = true;
                   while(flag){
                       try{
                    valorDesigualdad = JOptionPane.showInputDialog("\n"+"Ingrese la desigualdad [ 0: <=, 1: >=, 2: = ]");
                    if(Integer.valueOf(valorDesigualdad)>=0 && Integer.valueOf(valorDesigualdad)<=3) flag = false;
                       }
                       catch(NumberFormatException nfe){
                           flag = true;
                       }
                   }
                }
                

                try {
                    desigualdad = Integer.valueOf(valorDesigualdad);
                    String valorE;
                    if(constraint!=null){
                        valorE = JOptionPane.showInputDialog(constraint+(valorDesigualdad.equalsIgnoreCase("0")?"<=":valorDesigualdad.equalsIgnoreCase("1")?">=":"=")+"\nIngrese E");
                    }else{
                        valorE = JOptionPane.showInputDialog("Ingrese E");
                    }
                    //valorE = JOptionPane.showInputDialog("Ingrese E");
                    e = Double.valueOf(valorE);

                    //System.out.println("Paso 8 y Paso 9");

                    Map nine = selectMaxFunctionVectorGS1((double[]) six.get("vectorGS1"), objetivos, A, b, zkMin, zkMax, e);
                    double[][] newObjectives = (double[][]) nine.get("newObjectives");
                    //printM("newObjectives", newObjectives);
                    
                    constraint = vectorToString((double[])nine.get("objectiveRemoved"));
                    
                    //System.out.println("\nPaso 5");
                    dtmZ.removeRow(0);
                    dtmZ.removeRow(0);
                    dtmG.removeRow(0);
                    dtmG.removeRow(0);
                    five = calculateStepFive(newObjectives, (double[]) nine.get("newZKMin"), (double[]) nine.get("newZKMax"));
                    //gi=(double[][])five.get("gi");  no descomentar
                    constant = (Double) five.get("constant");
                    s = (double[]) five.get("s");


                    //System.out.println("\nPaso 6");
                    Map simplexR = applyRevisedSimplex(s.length, ((double[][]) nine.get("newA")).length, s, false, (double[][]) nine.get("newA"), (double[]) nine.get("newB"), desigualdad);

                    six = calculateStepSix((double[][]) nine.get("newA"), (double[]) nine.get("newB"), s, constant, objetivos, gi, true, (double[]) simplexR.get("solutions"));
                    //six = calculateStepSix((double[][])nine.get("newA"), (double[])nine.get("newB"), s, constant, newObjectives, gi); no descomentar



                    /*e = 695;
                     nine = selectMaxFunctionVectorGS1((double[]) six.get("vectorGS1"), objetivos, A, b, zkMin, zkMax, e);
                     newObjectives = (double[][]) nine.get("newObjectives");
                     printM("newObjectives", newObjectives);

                     System.out.println("\nPaso 5");
                     five = calculateStepFive(newObjectives, (double[]) nine.get("newZKMin"), (double[]) nine.get("newZKMax"));
                     //gi=(double[][])five.get("gi");
                     constant = (Double) five.get("constant");
                     s = (double[]) five.get("s");


                     System.out.println("\nPaso 6");
                     simplexR = applyRevisedSimplex(s.length, ((double[][]) nine.get("newA")).length, s, false, (double[][]) nine.get("newA"), (double[]) nine.get("newB"), 1);

                     six = calculateStepSix((double[][]) nine.get("newA"), (double[]) nine.get("newB"), s, constant, objetivos, gi, true, (double[]) simplexR.get("solutions"));

                     */
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "Valores no validos");
                }
            //}

        //} while (desicion((double[]) six.get("vectorZS1"), (double[]) six.get("vectorGS1"),zMax,(double[])six.get("vectorX1")));
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    public double[][] equivalence(double matriz[][]) {
        int size = matriz.length;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (matriz[i][j] < 0) {
                    matriz[i][j] = matriz[i][j] * (-1);
                }
            }
        }
        return matriz;
    }

    private static double[][] producto(double A[][], double B[][]) {
        double suma = 0;
        double result[][] = new double[A.length][B.length];
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < B.length; j++) {
                suma = 0;
                for (int k = 0; k < B.length; k++) {
                    suma += A[i][k] * B[k][j];
                }
                result[i][j] = suma;
            }
        }
        return result;
    }

    static double[][] trasponer(double[][] orig) {
        double[][] trasp = new double[orig[0].length][orig.length];

        for (int i = 0; i < orig.length; i++) {
            for (int j = 0; j < orig[i].length; j++) {
                trasp[j][i] = orig[i][j];
            }
        }

        return trasp;
    }

    public static double[][] calculateOptimalSolutions(double[][] results, double[][] objectives) {
        objectives = trasponer(objectives);
        return producto(results, objectives);
    }

    public static double[][] applySimplex(double objectives[][], double[][] restrictionsA, double[] restrictionsB) {
        Simplex simplex;
        double[] objective = new double[numeroVariables];
        double[] result = new double[numeroVariables];
        double[][] results = new double[numeroVariables][numeroVariables];
        for (int i = 0; i < numeroVariables; i++) {
            for (int j = 0; j < numeroVariables; j++) {
                objective[j] = objectives[i][j];
            }
            simplex = new Simplex(restrictionsA, restrictionsB, objective);
            result = simplex.primal();
            for (int j = 0; j < numeroVariables; j++) {
                results[i][j] = result[j];
            }
        }
        return results;
    }

    public static double[] findMax(double optimalSolutions[][]) {
        double ZMax[] = new double[numeroVariables];
        for (int i = 0; i < numeroVariables; i++) {
            double max = optimalSolutions[0][i];
            for (int j = 0; j < numeroVariables; j++) {
                if (optimalSolutions[j][i] > max) {
                    max = optimalSolutions[j][i];
                }
            }
            ZMax[i] = max;
        }
        return ZMax;
    }

    /**
     * Método que busca el valor mínimo de cada columna de la matriz de pagos.
     *
     * @param optimalSolutions
     * @return
     */
    public static double[] findMin(double optimalSolutions[][]) {
        double ZMin[] = new double[numeroVariables];
        for (int i = 0; i < numeroVariables; i++) {
            double min = optimalSolutions[0][i];
            for (int j = 0; j < numeroVariables; j++) {
                if (optimalSolutions[j][i] < min) {
                    min = optimalSolutions[j][i];
                }
            }
            ZMin[i] = min;
        }
        return ZMin;
    }

    public static double[] defineZMax(double[] zkMax) {
        return zkMax;
    }

    public static void printM(String titulo, double[][] M) {
        System.out.println("--------" + titulo + "---------");
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                System.out.print(M[i][j] + "\t");
            }
            System.out.println();
        }
        System.out.println("-----------------");

    }

    public static void printV(String titulo, double[] V) {
        System.out.println("--------" + titulo + "---------");
        for (int i = 0; i < V.length; i++) {
            System.out.print(V[i] + "\t");
            
        }
        System.out.println("\n-----------------");
    }

    public static double[] getSubstituteFunction(double[][] gi) {
        double[] s = new double[gi[0].length];
        for (int i = 0; i < gi[0].length; i++) {
            s[i] = 0;
            for (int j = 0; j < gi.length; j++) {

                s[i] += gi[j][i];
            }
        }
        return s;
    }

    public static double getConstant(double[] s) {
        return s[s.length - 1];
    }

    public static double[] getS(double[] sAux) {
        double[] s = new double[sAux.length - 1];
        for (int i = 0; i < sAux.length - 1; i++) {
            s[i] = sAux[i];
        }
        return s;
    }

    public static double[] generateFunctionZS1(double[][] objectives, double[] vectorX1) {
        double[] vectorZS1 = new double[objectives.length];
        double[] objective = new double[objectives.length];
        for (int i = 0; i < objectives.length; i++) {
            /*for (int j = 0; j < numeroVariables; j++) {
             objective[j] = objectives[i][j];
             }*/
            objective = objectives[i];
            for (int k = 0; k < objective.length; k++) {
                vectorZS1[i] += objective[k] * vectorX1[k];
            }
        }
        return vectorZS1;
    }

    public static double[] generateFunctionGS1(double[][] gi, double[] vectorX1) {
        double[] vectorGS1 = new double[gi.length];
        double[] giP = new double[gi.length];
        double constant = 0;
        //printM(gi);
        //printV(vectorX1);

        for (int i = 0; i < /*numeroVariables*/ gi.length; i++) {
            for (int j = 0; j < /*numeroVariables*/ gi.length; j++) {
                giP[j] = gi[i][j];
            }
            constant = gi[i][gi.length];
            //printV(giP);
            for (int k = 0; k < /*numeroVariables*/ gi.length; k++) {
                vectorGS1[i] += giP[k] * vectorX1[k];

            }
            vectorGS1[i] = vectorGS1[i] - constant;
            // System.out.println(vectorGS1[i]-constant);
        }
        return vectorGS1;
    }

    public static Map/*double[]*/ selectMaxFunctionVectorGS1(double[] vectorGS1, double[][] objectives, double[][] restrictionsA, double[] restrictionsB, double[] zKMin, double[] zKMax, double e) {
        double[] objective = new double[objectives[0].length];
        double max = 0;
        int maxIndex = 0;
        List objectivesAux = new ArrayList<Object>();
        for (int i = 0; i < objectives.length; i++) {
            objectivesAux.add(objectives[i]);
        }
        for (int i = 0; i < vectorGS1.length; i++) {
            if (vectorGS1[i] >= max) {
                max = vectorGS1[i];
                maxIndex = i;

            }
        }

        objective = (double[]) objectivesAux.remove(maxIndex);
        double[][] newObjectives = new double[objectivesAux.size()][objectives[0].length];
        int i = 0;
        for (Object object : objectivesAux) {
            newObjectives[i++] = (double[]) object;
        }

        double[][] a = new double[restrictionsA.length + 1][restrictionsA[0].length];
        double[] b = new double[restrictionsB.length + 1];
        for (int j = 0; j < restrictionsA.length; j++) {
            a[j] = restrictionsA[j];
        }


        a[a.length - 1] = objective;

        for (int j = 0; j < restrictionsB.length; j++) {
            b[j] = restrictionsB[j];
        }
        b[b.length - 1] = e;
        //printM("a", a);
        //printV("b", b);

        List zKMaxTemp = new ArrayList<Object>();
        for (int j = 0; j < zKMax.length; j++) {
            zKMaxTemp.add(zKMax[j]);
        }
        zKMaxTemp.remove(maxIndex);
        List zKMinTemp = new ArrayList<Object>();
        for (int j = 0; j < zKMin.length; j++) {
            zKMinTemp.add(zKMin[j]);
        }
        zKMinTemp.remove(maxIndex);

        double[] newZKMax = new double[zKMax.length - 1];
        double[] newZKMin = new double[zKMin.length - 1];
        i = 0;
        for (Object object : zKMaxTemp) {
            newZKMax[i++] = (Double) object;
        }
        i = 0;
        for (Object object : zKMinTemp) {
            newZKMin[i++] = (Double) object;
        }



        Map objectiveRemoved_newObjectives = new HashMap();
        objectiveRemoved_newObjectives.put("objectiveRemoved", objective);
        objectiveRemoved_newObjectives.put("newObjectives", newObjectives);
        objectiveRemoved_newObjectives.put("newA", a);
        objectiveRemoved_newObjectives.put("newB", b);
        objectiveRemoved_newObjectives.put("newZKMin", newZKMin);
        objectiveRemoved_newObjectives.put("newZKMax", newZKMax);


        //return objective;
        return objectiveRemoved_newObjectives;

    }

    public static double[][] generateGi(double[][] objectives, double[] zKMin, double[] zKMax) {

        //printM("Objectives",objectives);
        double[] objective;
        double[][] gi = new double[objectives.length][objectives[0].length + 1];
        for (int i = 0; i < objectives.length; i++) {
            objective = objectives[i];

            double rango = zKMax[i] - zKMin[i];
            System.out.println("Rango " + rango);
            for (int j = 0; j < objective.length; j++) {
                gi[i][j] = objective[j] / rango;
            }
            gi[i][objective.length] = zKMin[i] / rango;
        }

        //printM("gi", gi);
        return gi;
    }

    public static Map calculateStepFive(double[][] objectives, double[] zKMin, double[] zKMax) {
        Map five = new HashMap();
        double[][] gi = generateGi(objectives, zKMin, zKMax);

        double[] sAux = getSubstituteFunction(gi);
        //printV("Saux", sAux);
        double constant = getConstant(sAux);

        double[] s = getS(sAux);
        //printV("S", s);
        //System.out.println("Constant " + constant);
        five.put("gi", gi);
        five.put("constant", constant);
        five.put("s", s);
        return five;
    }

    public static Map calculateStepSix(double[][] a, double[] b, double[] s, double constant, double[][] objectives, double[][] gi, boolean methodSR, double[] vectorXn) {
        Map six = new HashMap();
        Map simplexR = new HashMap();
        double[] vectorX1;
        String value;
        if (methodSR) {
            vectorX1 = vectorXn;
            value = "Pendient SimplexR";
        } else {
            Simplex simplex = new Simplex(a, b, s);
            vectorX1 = simplex.primal();
            value = ((Double) (simplex.value() - constant)).toString();
        }

        //printV("VectorX1", vectorX1);
        //System.out.println("Value " + value);

        double[] vectorZS1 = new double[objectives.length];
        vectorZS1 = generateFunctionZS1(objectives, vectorX1);
        //printV("VectorZS1", vectorZS1);
        DatosTablaZ(vectorZS1);
        


        double[] vectorGS1 = generateFunctionGS1(gi, vectorX1);
        //printV("VectorGS1", vectorGS1);
        DatosTablaG(vectorGS1);

        six.put("vectorX1", vectorX1);
        six.put("vectorZS1", vectorZS1);
        six.put("vectorGS1", vectorGS1);
        return six;
    }
    
    public static void DatosTablaZ(double[] vector){
         for (int i = 0; i < vector.length; i++) {
             dtmZ.addRow(new Object[] {vector[i]});
         
         }
         
    }
    
    public static void DatosTablaG(double[] vector){
         for (int i = 0; i < vector.length; i++) {
             
             dtmG.addRow(new Object[] {vector[i]});
         
         }
    }

    public static Map applyRevisedSimplex(int numberVariables, int numberConstraints, double[] objective, boolean min, double[][] a, double[] b, int inequality) {
        //min=true
        //inequality of the new constraint = {0:<=,1:>=,2:=}

        Map simplexR = new HashMap();
        double[] solutions = new double[numberVariables];
        float[] obj = new float[objective.length];
        for (int i = 0; i < objective.length; i++) {
            obj[i] = (new Double(objective[i])).floatValue();
        }
        float[][] newA = new float[a.length][a[0].length];
        for (int i = 0; i < newA.length; i++) {
            for (int j = 0; j < newA[0].length; j++) {
                newA[i][j] = (new Double(a[i][j])).floatValue();
            }
        }

        float[] newB = new float[b.length];
        for (int i = 0; i < newB.length; i++) {
            newB[i] = (new Double(b[i])).floatValue();
        }

        RevisedSimplex revisedSimplex = new RevisedSimplex(numberVariables, numberConstraints);
        revisedSimplex.reset(numberVariables, numberConstraints);
        revisedSimplex.specifyObjective(obj, min);

        for (int i = 0; i < newA.length - 1; i++) {
            revisedSimplex.addConstraint(newA[i], newB[i], 0);
        }
        revisedSimplex.addConstraint(newA[newA.length - 1], newB[newB.length - 1], inequality);

        revisedSimplex.preprocess(numberVariables, numberConstraints);

        int state = 0;
        boolean ok = false;
        while (!ok) {
            state = revisedSimplex.iterate();
            switch (state) {
                case 3:
                    System.out.println("The problem is unbounded.");
                    break;
                case 2:
                    System.out.println("The problem is Feasible.");
                    break;

                case 1:
                    if (revisedSimplex.ArtificialAdded == false) {
                        //System.out.println("You've Done It!\nYou've Solved It!!!");
                        ok = true;

                    } else {  /* artificial variables were added (in phase I) */
                        /* if it passes this if test, then the problem is feasible */

                        /**
                         * * ADD CODE HERE TO TEST WHEN ARTIFICIALS ARE STILL
                         * IN BASIS **
                         */
                        if (revisedSimplex.calculateObjective() == 0) {

                            if (revisedSimplex.NumArtificials > 0) {
                                //System.out.print("Even though 0 objective, there are");
                                //System.out.println(" still artificials in basis.");
                            }

                            //System.out.println("Calling getRidOfArtificials");
                            revisedSimplex.getRidOfArtificials();
                            //System.out.println("Called  getRidOfArtificials");

                        } else {

                            //System.out.println("The Problem is infeasible.");
                        }
                    }
                    break;
                case 0:
                    break;

            }

            /*for (int i = 0; i < revisedSimplex.numConstraints; i++){
             System.out.println("x"+(revisedSimplex.BasicVariables[i]+1)+"= "+Float.toString(revisedSimplex.x[i]));
             }*/
            for (int i = 0; i < numberVariables; i++) {
                solutions[i] = (double) revisedSimplex.x[i];
            }
        }

        simplexR.put("solutions", solutions);

        return simplexR;

    }

    public static boolean desicion(double[] vectorZS, double[] vectorGS, double[] zMax,double[] solution) {
        String mensaje = "ZS               GS             ZMax\n";
        DecimalFormat df = new DecimalFormat("#0.00");
        for (int i = 0; i < vectorZS.length; i++) {
            mensaje += df.format(vectorZS[i]) + "          "+df.format(vectorGS[i]) +"           "+zMax[i] + "\n";
        }
        //mensaje += "GS ";
        //for (int i = 0; i < vectorGS.length; i++) {
          //  mensaje += vectorGS[i] + " ";
        //}
        String yes = JOptionPane.showInputDialog(mensaje +" \nLa solución es satisfactoria? (N/Y)");
        
        
        if ("N".equalsIgnoreCase(yes)) {
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "Solution\n"+vectorToStringSolution(solution));
            return false;
        }
    }
    
    public static String vectorToString(double[] vector){
        String s="";
        for(int i=0;i<vector.length;i++){
            s+= vector[i]+"X"+(i+1)+" ";
        }
        return s;
    }
    
    public static String vectorToStringSolution(double[] solution){
        String s="";
        for(int i=0;i<solution.length;i++){
            s+= "X"+(i+1)+"="+solution[i]+" ";
        }
        return s;
    }
    
    //*************************************************************************************************************
    //*************************************************************************************************************
    //*************************************************************************************************************
    //*************************************************************************************************************
    
    
    
    
    

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaZ = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaG = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        SolucionBT = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        NuevaIteracionBT = new javax.swing.JButton();

        setBackground(java.awt.SystemColor.inactiveCaptionBorder);

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Algoritmo de Goicochea [Resultados]");

        jLabel2.setText("Vector Z:");

        jLabel3.setText("Vector G:");

        tablaZ.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Valores"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaZ);
        tablaZ.getColumnModel().getColumn(0).setResizable(false);

        tablaG.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Valores"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tablaG);
        tablaG.getColumnModel().getColumn(0).setResizable(false);

        jLabel4.setText("Solucion Satisfactoria?");

        SolucionBT.setBackground(new java.awt.Color(153, 180, 209));
        SolucionBT.setText("SI");
        SolucionBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SolucionBTActionPerformed(evt);
            }
        });

        jButton1.setText("Escuchar");

        NuevaIteracionBT.setText("NO");
        NuevaIteracionBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NuevaIteracionBTActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 212, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(56, 56, 56))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SolucionBT)
                .addGap(18, 18, 18)
                .addComponent(NuevaIteracionBT)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(9, 9, 9)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SolucionBT)
                    .addComponent(jLabel4)
                    .addComponent(NuevaIteracionBT))
                .addGap(5, 5, 5))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void SolucionBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SolucionBTActionPerformed
        // TODO add your handling code here:
        try{
            JOptionPane.showMessageDialog(null, "Solución\n"+vectorToStringSolution((double[])six.get("vectorX1")));
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_SolucionBTActionPerformed

    private void NuevaIteracionBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NuevaIteracionBTActionPerformed
        
        decisionFinal();
    }//GEN-LAST:event_NuevaIteracionBTActionPerformed

    public void resultados(){
        
        
        dtmZ = new DefaultTableModel(new Object[][] {}, new String[] {"Valor"}){
            @SuppressWarnings("rawtypes")
			Class[] types = new Class[] { java.lang.Integer.class};
            @SuppressWarnings({ "rawtypes", "unchecked" })
			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}
		};
        
        dtmG = new DefaultTableModel(new Object[][] {}, new String[] {"Valor"}){
            @SuppressWarnings("rawtypes")
			Class[] types = new Class[] { java.lang.Integer.class};
            @SuppressWarnings({ "rawtypes", "unchecked" })
			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}
		};
        
        
        //dtmZ.addRow(new Object[] {950});
        //dtmZ.addRow(new Object[] {129});
        
        tablaZ.setModel(dtmZ);
        tablaG.setModel(dtmG);
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton NuevaIteracionBT;
    private javax.swing.JButton SolucionBT;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tablaG;
    private javax.swing.JTable tablaZ;
    // End of variables declaration//GEN-END:variables
}
